<?php
class Default_Model_ThongTinCtyMapper {

	protected $_dbTable;
	protected $db;

	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Default_Model_DbTable_ThongTinCty');
		}
		return $this->_dbTable;
	}

	public function getDb(){
		if ($this->db === null) {
			$this->db = Zend_Registry::get('connectDB');
		}
		return $this->db;
	}
	
	/*
	 * processResultSet
	 */
	private function processResultSet($resultSet) {
		$entries = array();
		foreach ($resultSet as $row) {
			$entries[$row['id']] = $row['noi_dung'];
		}
		return $entries;
	}
	
	/*
	 * fetch
	 */
	public function fetchAll($query = null, $category = 0) {
         $table = $this->getDbTable();
         $select = $table->select();
         $select->from($table);
		 if ($query === NULL) {
		 	$select->where('loai = ' . $category);
        } else {
            $select->where($query . ' AND loai = ' . $category);
        }
        $resultSet = $this->getDbTable()->fetchAll($select);
        return $this->processResultSet($resultSet->toArray());
	}
}